# -*- coding:utf-8 -*-
import pymongo
from bson.code import Code
import mongoDB

collections = "dmarket_access_log"
collection = mongoDB.connMongo(collections)

mapper = Code("""function() {
        emit({imei:this.imei, u:this.u}, {count:1});
}
""")

reducer = Code(""" function (key, values) {
    var cnt = 0;
    values.forEach(function(val) { cnt += val.count;});
    return {"count":cnt};
}
""")

result = collection.map_reduce(mapper, reducer, out = "uv_stat_20170104", full_response=True, query={"u":{"$exists":"true"}, "day":{"$gt":"20161228", "$lte":"20170104"}})

print result

ret = collection.myresult.find()
print ret
